<?php

header ('Content-type: text/html; charset=UTF-8'); 


?>

<script src="system.js"></script>
<script src="md5-min.js"></script>

<script language="javascript" type="text/javascript">

			
	function submitbutton(pressbutton) 
	{
		//var form = document.adminForm;
	
		switch( pressbutton )
		{
			
			case	"send":
			{
				
					
					//form.btnsave.disabled = true;					
					//form.task.value=pressbutton;

					
					var oObject = {} ;
					
					oObject["id"] = getInputValueElementId ("id" );
					oObject["named"] = getInputValueElementId ("named" );
					oObject["detail"] = encodeURIComponent(getInputValueElementId( "detail" ));
					oObject["json_rule"] = encodeURIComponent(getInputValueElementId( "json_rule" ));
					oObject["execute_order"] = getInputValueElementId( "execute_order" );
					oObject["view_id"] = getInputValueElementId( "view_id" );
					oObject["component"] = "etl";
					oObject["controller"] = "transformation";
					oObject["task"] = "save";
					
					
					var jsonVales	=	convertToJSON(oObject);
					
					parameters	=	'parameters='+encodeURIComponent(
											convertToStringJSON(jsonVales));
					
					var method='';
					
					if( getInputValueElementId( "id" ) =='')
						method='POST';
					else
						method='PUT';
						
						
					httpRequest ( method,
									url_API,
									parameters,
									'retornoEditItemViewTransformation');
					
			
						
					return false;
				
				
			}break;
			case	"delete":
			{
				

					var oObject = {} ;
					
					oObject["id"] = getInputValueElementId ("id" );
					oObject["component"] = "etl";
					oObject["controller"] = "transformation";
					oObject["task"] = "delete";
					
					
					var jsonVales	=	convertToJSON(oObject);
					
					parameters	=	'parameters='+encodeURIComponent(
											convertToStringJSON(jsonVales));
					
					var method='';
					
					if( getInputValueElementId( "id" ) !=''){ 
						
						method='DELETE';
						
						httpRequest ( method,
									url_API,
									parameters,
									'retornoEditItemViewTransformation');
					}


		
					return false;
				
				
			}break;
			default:
			{
				
			}
		}	
		
	}


function retornoEditItemViewTransformation(jsonObj){
		
	if( jsonObj.status == 1){
		
		//alert( jsonObj.data );
		window.location.href='transformation_list.php?view_id='+getURIParameter('view_id');
			
	}else{
		
		alert( jsonObj.data );
	}

}







function returnItemData(jsonObj){
	


	var obj_json = JSON.parse(jsonObj.data);

	document.getElementById("id").value = obj_json[0].id;
	document.getElementById("named").value = obj_json[0].named;
	document.getElementById("view_id").value = obj_json[0].view_id;
	document.getElementById("detail").value = decodeURIComponent(obj_json[0].detail);
	document.getElementById("json_rule").value = decodeURIComponent(obj_json[0].json_rule);
	document.getElementById("execute_order").value = obj_json[0].execute_order;

	JSON_rules(decodeURIComponent(obj_json[0].json_rule));
}



function getItem(id) 
{

	if( id ==="null" 
		|| id===null 
		|| id==="" 
		|| typeof id === "undefined"){

		
	}else{
		
		var oObject = {} ;
						
		oObject["id"] = id;
		oObject["component"] = "etl";
		oObject["controller"] = "transformation";
		oObject["task"] = "getItem";

		var jsonVales	=	convertToJSON(oObject);

		parameters	=	'parameters='+encodeURIComponent(
								convertToStringJSON(jsonVales));

		var method='GET';

		httpRequest ( method,
						url_API,
						parameters,
						'returnItemData');
						
	}

					
}



function getItemViewColumnsTable(key) 
{

	if( key!=''){

		var oObject = {} ;
						
		oObject["key"] = key;//hex_md5(id);
		oObject["component"] = "etl";
		oObject["controller"] = "view";
		oObject["task"] = "getColumnsTable";

		var jsonVales	=	convertToJSON(oObject);

		parameters	=	'parameters='+encodeURIComponent(
								convertToStringJSON(jsonVales));

		var method='GET';

		httpRequest ( method,
						url_API,
						parameters,
						'returnMetadataColumnsSELECT');
	}

					
}



function returnMetadataColumnsSELECT(jsonObj){
	

	if( jsonObj.status == 1){
		

		var grid_view_html="";
		var obj_json = JSON.parse(jsonObj.data);
		var i;
		
		grid_view_html ="<select id='column_table' name='column_table'>";
		grid_view_html = grid_view_html + "<option value='-1'>-- Selecione --</option>";

		
		for (i=0; i < obj_json.length; i++){

		    		
		    for (var key in obj_json[i]) {
			    	
				grid_view_html = grid_view_html +"<option value='"+obj_json[i][key]+"'>"+obj_json[i][key]+"</option>";
			    	
			}

		}


		document.getElementById("itens_columns").innerHTML = ""+grid_view_html+"</select>";

	}else{
		
		alert(jsonObj.data);
		

	}
	

	
}

var json_rules;

function JSON_rules( jsondata ){
	
	//var json_rules = JSON.parse(jsondata);

	//document.getElementById("id").value = obj_json[0].id;
	//document.getElementById("metadata").value = obj_json[0].metadata;
	
	//json_rules = obj_json[0].metadata;

	if( jsondata ==="null" 
		|| jsondata===null 
		|| jsondata==="" 
		|| typeof jsondata === "undefined"  ){
			
		json_rules = JSON.parse("[]"); 
	}else{
		json_rules = JSON.parse(jsondata); 
	}
}


function AddItenJSON(column, from, to){


	var ruleExist=false;
	
	var oObject = {} ;
				
	oObject["column"] =column.options[column.selectedIndex].text;
	oObject["rule"] = "convert";
	oObject["from"] = encodeURIComponent(from.value);
	oObject["to"] = encodeURIComponent(to.value);

	json_rules[json_rules.length]	=	convertToJSON(oObject);
		

	
	document.getElementById("json_rule").value = convertToStringJSON(json_rules);
	
}


</script>

<h1>Editar Transformação</h1>
<div id="text-result-view" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden-username" style="visibility:hidden;width:1px;height:1px;"></div>

<div id="text-result">Area restria aos usuarios autenticados. voce esta logado como <div id="username-id"></div>.<a href="#" onclick="javascript:logout();">Clique aqui para Logout</a></div>
 
<br>


<a href="home_authenticated.php">Página inicial</a><br>
<a href="javascript:window.location.href='transformation_list.php?view_id='+getURIParameter('view_id');">Lista de Transformações</a><br><br>



<form class="cmxform" action="#" method="GET">

	<div id="label">id</div> <div id="field"><input type="text" id="id" name="id" disabled=""/></div>
	<div id="label">view_id</div> <div id="field"><input type="text" id="view_id" name="view_id" disabled=""/></div>
	
	<div id="label">nome</div> <div id="field"><input type="text" id="named" name="named"/></div>
	<div id="label">detalhe</div> <div id="field">
		<textarea id="detail" name="detail" style="width:500px;height:50px;"></textarea>		
	</div>
	
	<div id="label">execute_order</div> <div id="field"><input type="text" id="execute_order" name="execute_order"/></div>
	
	
	
	
	<div id="label">Coluna</div> <div id="field">
		
		
		<table>
			<tr>
				<td align="left" colspan="4">
					<div id="itens_columns"></div>	
				</td>
			</tr>
			<tr>
				<td>
					De</td>
				<td>
					<input type="text" id="from" name="from" />
				</td>
				<td>
					Para</td>
				<td>
					<input type="text" id="to" name="to" /> <input type="button" name="Add" value="Add" onclick="javascript:AddItenJSON(document.getElementById('column_table'),document.getElementById('from'),document.getElementById('to'));"/>
				</td>
			</tr>
		</table>	
		
	</div>
	
	
	<div id="label">json_rule</div> <div id="field">
		<textarea id="json_rule" name="json_rule" style="width:500px;height:50px;"></textarea>		
	</div>
	
	
	<input type="button" name="enviar" value="Salvar" onclick="javascript:submitbutton('send');"/>
	<input type="button" name="excluir" value="Excluir" onclick="javascript:submitbutton('delete');"/>
	
	<input type="button" name="cancelar" value="Cancelar" onclick="javascript:window.location.href='transformation_list.php?view_id='+getURIParameter('view_id');"/>
	
</form>


<script>
getUsername();
getItem(getURIParameter('id'));
getItemViewColumnsTable( hex_md5(getURIParameter('view_id')) );

document.getElementById("view_id").value = getURIParameter('view_id') ;
</script>